﻿using System;
using System.Collections.Generic;
using System.Text;
using HarmonyLib;
using UnityEngine;
using BepInEx;
using Blis.Client;
using Blis.Server;
using System.Linq;
using Blis.Common;

namespace BlackSurvivalCheat
{
    
    static class HookFocusUI
    {
        static bool maintoggle;
        static List<PatchHandler> handles;
        public static int itemcode;
        static HookFocusUI()
        {
            maintoggle = true;
            itemcode = 0;

            handles = new List<PatchHandler>();
            handles.Add(new PatchHandler { Postfix = AccessTools.Method(typeof(HookFocusUI), "NavigationStore_GetNeedFocusSourceItems_Postfix"), TargetType = typeof(Blis.Client.UI.NavigationStore), TargetMethonName = "GetNeedFocusSourceItems" });
        }

        public static void Load(Harmony harmony)
        {
            foreach (PatchHandler handler in handles)
            {
                handler.Patch(harmony);
            }
        }

        public static void UnLoad(Harmony harmony)
        {
            foreach (PatchHandler handler in handles)
            {
                handler.Unpatch(harmony);
            }
        }

        static void NavigationStore_GetNeedFocusSourceItems_Postfix(IEnumerable<KeyValuePair<Blis.Common.ItemData, int>> __result)
        {
            if (!maintoggle)
                return;
            if (__result != null)
            {
                int level = -1;
                itemcode = 0;
                foreach (KeyValuePair<Blis.Common.ItemData, int> pair in __result)
                {
                    if ((int)pair.Key.itemGrade > level)
                    {
                        itemcode = pair.Key.code;
                        level = (int)pair.Key.itemGrade;
                    }
                }
            }
        }

    }
}
